package services;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DataAccess.DeviceDA;
import DataAccess.NguoiDungDA;
import DataAccess.NodeThaoLuanDA;

import pojo.DevicePOJO;
import pojo.NguoiDungPOJO;
import pojo.NodeThaoLuanPOJO;
import pojo.PairValue;
import util.ClientSender;
import util.Common;

/**
 * Servlet implementation class SendAllServlet
 */
@WebServlet("/SendAllServlet")
public class SendAllServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SendAllServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
//		DeviceDA da = new DeviceDA();
//		ArrayList<DevicePOJO> lstDV = da.get();
//		ArrayList<NguoiDungPOJO> lstNguoiDung=new NguoiDungDA().getNguoiDungCuaCay(1);
//		ArrayList<String> lstReg=Common.retrieveRegId(lstNguoiDung);
		NodeThaoLuanPOJO node=new NodeThaoLuanDA().get(2);
//		ArrayList<PairValue> params=new ArrayList<PairValue>();
//		params.add(new PairValue(Protocol.CMD,Protocol.THEM_NODE));
//		params.add(new PairValue(Protocol.ID_CAY,node.getIdCay()+""));
//		params.add(new PairValue(Protocol.VERSION, node.getVersion()+""));
//		try {
//			ClientSender.send(lstReg, params,20);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		ArrayList<NguoiDungPOJO> lstNguoiDung=new NguoiDungDA().getNguoiDungCuaCay(1);
		ArrayList<PairValue> params=new ArrayList<PairValue>();
		params.add(new PairValue(Protocol.CMD,Protocol.THEM_NODE));
		params.add(new PairValue(Protocol.ID_CAY,node.getIdCay()+""));
		params.add(new PairValue(Protocol.VERSION, node.getVersion()+""));
		
		try {
			//ClientSender.send(Common.retrieveRegId(lstNguoiDung), params, 30);
			ArrayList<String> lstRegId=Common.retrieveRegId(lstNguoiDung);
			ClientSender.send(lstRegId, params, 30);
		} catch (Exception e) {
			System.out.println("Loi:"+e.getMessage());
		}
//		if (lstDV != null) {
//			try {
//				for (int i = 0; i < lstDV.size(); i++) {
//					PairValue p = new PairValue("test", "test" + 1);
//					ClientSender.send(lstDV.get(i).getRegID(), p);
//
//				}
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
	}

}
